import { getVersion } from '@/api/version'
import { listByQuery } from '@/api/pwd'
import { useOssStore } from '@/stores/modules/oss'
import { usePwdListStore } from '@/stores'

export default function() {
  let ossStore = useOssStore()
  let pwdListStore = usePwdListStore()

  const syncRemoteData = () => {
    console.log('syncRemoteData')
    //  获取版本号, 如果版本号和当前版本不一致,则从服务器拉取数据
    let token = ossStore.getToken()
    console.log('token',token)
    getVersion(token).then(res => {
      console.log('getVersion 查询结果', res)
      if (res.code !== '0') {
        console.log('getVersion 查询失败', res)
        return
      }
      if (pwdListStore.getVersion() === res.data) {
        return
      }
      console.log(`本地版本号和服务器不一致, 重新拉取数据, 本地版本号:${pwdListStore.getVersion()},服务器版本号:${res.data}`)
      pwdListStore.setVersion(res.data)
      listByQuery({ token: token }).then(res => {
        console.log('listByQuery 查询结果', res)
        if (res.data) {
          pwdListStore.setList(res.data)
        }
      })
    })
  }

  function search(res: any) {
    console.log('----search confirm:', res)
    // 跳转到列表页面
    // uni.switchTab({
    //   url: `/pages/table/index`,
    // })
  }

  function input(res: any) {
    console.log('----input:', res)
  }

  function clear() {
    console.log('----clear:')
    pwdListStore.clearSearch()

  }

  function cancel(res: any) {
    console.log('----cancel:', res)
    pwdListStore.clearSearch()
  }

  return { syncRemoteData, search, input, clear, cancel }
}